﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;


namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for danhmuc.xaml
    /// </summary>
    public partial class danhmuc : UserControl
    {
        #region KhaiBaoBien
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA25";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0200";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc Vinh Phuc";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc Vinh Phuc";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        string strThongBao = "",MASOGOC ="",MASONGON="";
        string strThemSuaXoa = "";
        string strTim = "";//"CoDuLieu"; "KhongCoDuLieu"
        Boolean kt;
        Int16 p_thang, ifthang;
        Int16 p_nam, ifnam, row, bieu;
        int TT,i;
        string dem;
        int dem1;
        int intDongTrenGrid;
        DataSet ds;
        DataSet dshuynhap;
        DataSet dsbieu05_Entity;
        DataSet dsbieu00_Entity;
        ListItem lstItem;
        DataTable dtHienThi = new DataTable();
        DataTable dtbieu00 = new DataTable();
        #endregion

        //============================================================================================================================
        #region KhoiTaoThongTinKhiLoadForm

        private void setEntity()
        {
            try
            {
                //case "2": //Loadgrid
                        dsbieu00_Entity.Tables["PC1_DANHMUC"].Rows.Clear();
                        DataRow dr1 = dsbieu00_Entity.Tables["PC1_DANHMUC"].NewRow();
                        //dr1["DOI_TUONG"] = " ";
                        //dr1["TS_KH"] = "";
                        //break;
                        dsbieu00_Entity.Tables["PC1_DANHMUC"].Rows.Add(dr1);
                        dsbieu00_Entity.AcceptChanges();
                        //break;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void Initgrid(string ma_donvi)
        {
           setEntity();
            if (ma_donvi.ToString().Length == 4)
            {
                //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS1 = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
                //DataSet dscobocty = GlobalServices.Service_NPCUS.Chon_cty_dienluc(p_nam, p_thang, ma_donvi, 18);
            }
            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
            //DataSet dscobo = cls.BCKD00_DANHMUC(ma_donvi, 0);
            bieu = 0;
            DataSet dscobo = GlobalServices.Service_NPCUS.BCKD00_DANHMUC(ma_donvi, bieu);
            //dshuynhap = dscobo.Copy();
            dtbieu00 = dscobo.Tables[0].Copy();
            //dtbieu00.Columns["DVT"].
            //DataSet dscobo = GlobalServices.Service_NPC.select_donviqly(dsD_BCKD_Entity);
            if (dtbieu00.Rows.Count > 0)
            {
                //DataTable dtTemp = dsD_BCKD_Entity.Tables["PC1_D_BCKD"].Copy();                            
                //dtHienThi = dtTemp;
                //strTim = "CoDuLieu";
                dgr_danhmuc.ItemsSource = dtbieu00.DefaultView;
                // CheckIsEnableButtonSuaXoa();
                dgr_danhmuc.SelectedIndex = 0;
                //txtPT_DG.Text = dtbieu00.Rows[1]["PHANTICH_DANHGIA"].ToString();
                //txtYKIEN_KHAC.Text = dtbieu00.Rows[1]["YKIEN_KHAC"].ToString();
            }
        }
        #endregion
        public danhmuc()
        {
            InitializeComponent();
            dsbieu00_Entity = new BCNPCEntities.BCKD15.PC1_D_BCKD_Entity();
            dtbieu00 = dsbieu00_Entity.Tables["PC1_DANHMUC"].Clone();
            //Initgrid(strMa_dviqly);
            dgr_danhmuc.IsReadOnly = true;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cmdghi.IsEnabled = false;
            cmdthemmoi.IsEnabled = false;
            cmdxoa.IsEnabled = false;
            cmdsua.IsEnabled = false;
            txtdanhmuc.IsEnabled = false;
            txtstt.IsEnabled = false;
            txtmaso.IsEnabled = false;
            txtdvt.IsEnabled = false;
        }

        private void dgr_danhmuc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgr_danhmuc.SelectedIndex > 0)
            {
                txtdanhmuc.Text = dtbieu00.Rows[dgr_danhmuc.SelectedIndex]["DM_NAME"].ToString();
                txtstt.Text = dtbieu00.Rows[dgr_danhmuc.SelectedIndex]["STT"].ToString();
                txtmaso.Text = dtbieu00.Rows[dgr_danhmuc.SelectedIndex]["MA_SO"].ToString();
                txtdvt.Text = dtbieu00.Rows[dgr_danhmuc.SelectedIndex]["DONVI_DO"].ToString();
            }
        }

        private void cmdthemmoi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dem = "them"; // THEM MOI
                switch (cmdthemmoi.Content.ToString())
                {
                    case "Thêm mới":
                        cmdghi.IsEnabled = true;
                        cmdsua.IsEnabled = false;
                        cmdxoa.IsEnabled = false;
                        button1.IsEnabled = false;
                        cmdthemmoi.Content = "Bỏ qua";
                        txtdanhmuc.IsEnabled = true;
                        txtstt.IsEnabled = true;
                        txtmaso.IsEnabled = true;
                        txtdvt.IsEnabled = true;
                        txtstt.Focus();
                        break;
                    case "Bỏ qua":
                        cmdghi.IsEnabled = false;
                        cmdsua.IsEnabled = true;
                        cmdxoa.IsEnabled = true;
                        button1.IsEnabled = true;
                        cmdthemmoi.Content = "Thêm mới";
                        txtdanhmuc.IsEnabled = false;
                        txtstt.IsEnabled = false;
                        txtmaso.IsEnabled = false;
                        txtdvt.IsEnabled = false;
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
          }

        private void cmdsua_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dem = "sua"; // SUA DANH MUC
                switch (cmdsua.Content.ToString())
                {
                    case "Sửa":
                        cmdghi.IsEnabled = true;
                        cmdthemmoi.IsEnabled = false;
                        cmdxoa.IsEnabled = false;
                        button1.IsEnabled = false;
                        cmdsua.Content = "Bỏ qua";
                        txtdanhmuc.IsEnabled = true;
                        txtstt.IsEnabled = false;
                        txtmaso.IsEnabled = false;
                        txtdvt.IsEnabled = true;
                        txtdvt.Focus();
                        break;
                    case "Bỏ qua":
                        cmdghi.IsEnabled = false;
                        cmdthemmoi.IsEnabled = true;
                        button1.IsEnabled = true;
                        cmdxoa.IsEnabled = true;
                        cmdsua.Content = "Sửa";
                        txtdanhmuc.IsEnabled = false;
                        txtstt.IsEnabled = false;
                        txtmaso.IsEnabled = false;
                        txtdvt.IsEnabled = false;
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void kiem_tra(string maso)
        {
            TT = 0;
            for (int i = 0; i < dtbieu00.Rows.Count; i++) 
            {
                if (maso == dtbieu00.Rows[i]["MA_SO"].ToString())
                {
                    TT = 1;
                }
            }
        }
        private void indong(string ma_so)
        {
          //  kt = false;
            for (int i = 0; i < dtbieu00.Rows.Count; i++)
            {
                if (dtbieu00.Rows[i]["MA_SO"].ToString().Trim() == ma_so)
                    {
                       intDongTrenGrid = i;
                        break;
                    }
            }
        }
        private void cmdghi_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = dgr_danhmuc.SelectedItem as DataRowView;  
            try
            {
                strThongBao = txtmaso.Text.ToString(); 
                for (int k = 0; k < 1; k++)
                {
                    if (dem.ToString() == "them")
                    {
                        if (txtstt.Text.Trim() == "")
                            {
                                MessageBox.Show("Số thứ tự không để trống");
                                txtstt.Focus();
                                break;
                            }
                        if (txtmaso.Text.Trim() == "")
                            {
                                MessageBox.Show("Mã số không được để trống");
                                txtmaso.Focus();
                                break;
                            }
                        if (txtdanhmuc.Text.Trim() == "")
                            {
                                MessageBox.Show("Danh mục không được để trống");
                                txtdanhmuc.Focus();
                                break;
                            }
                       kiem_tra(txtmaso.Text.Trim());
                            if (TT == 1)
                            {
                                MessageBox.Show("Mã số này đã tồn tại");
                                txtmaso.Focus();
                                break;
                            }

                            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
                            //cls.INSERT_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                         GlobalServices.Service_NPCUS.INSERT_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                        cmdthemmoi.Content = "Thêm mới";
                       // GlobalServices.Service_NPCUS.CapnhatdsloTT_KH(p_madvi)
                    }
                    if (dem.ToString() == "sua")
                    {
                        if (txtdanhmuc.Text.Trim() == "")
                        {
                            MessageBox.Show("Danh mục không được để trống");
                            txtdanhmuc.Focus();
                            break;
                        }
                        //strThongBao = txtmaso.Text.ToString();
                        //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
                        //cls.UPDATE_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                        GlobalServices.Service_NPCUS.UPDATE_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                        cmdsua.Content = "Sửa";
                        // GlobalServices.Service_NPCUS.CapnhatdsloTT_KH(p_madvi)
                    }
                    Initgrid(strMa_dviqly);
                    indong(strThongBao);
                    dgr_danhmuc.SelectedIndex = intDongTrenGrid;
                    cmdghi.IsEnabled = false;
                    cmdthemmoi.IsEnabled = true;
                    cmdsua.IsEnabled = true;
                    cmdxoa.IsEnabled = true;
                    button1.IsEnabled = true;
                    txtdanhmuc.IsEnabled = false;
                    txtstt.IsEnabled = false;
                    txtmaso.IsEnabled = false;
                    txtdvt.IsEnabled = false;
                }
                //dsbieu00_Entity = new BCNPCEntities.BCKD15.PC1_D_BCKD_Entity();
                //dtbieu00 = dsbieu00_Entity.Tables["PC1_DANHMUC"].Clone();
                  
                        //break;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }      
        }
        private void kiemtraxoa(Int16 dgrow)
        {
            dem1 = 0;
            //dtbieu00.Rows.Remove(dgrow);
            MASOGOC = dtbieu00.Rows[dgrow]["MA_SO"].ToString().Trim();
            MASONGON = MASOGOC.Substring(1, MASOGOC.Length - 1);
            for (int i = 0; i < dtbieu00.Rows.Count; i++)
            {
                if (MASONGON == "0000")
                {
                    if (dtbieu00.Rows[i]["MA_SO"].ToString().Trim().Substring(0, 1) == MASOGOC.Substring(0, 1))
                    {
                        dem1 = dem1 + 1;
                    }
                }

            }
        }
        private void cmdxoa_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dem = "xoa";
                DataRowView rowView = dgr_danhmuc.SelectedItem as DataRowView;
                DataRow rowdel = rowView.Row;
                //ds.Tables["Customer"].Rows.Remove(row); 
                row = Convert.ToInt16(dgr_danhmuc.SelectedIndex);
                if (dtbieu00.Rows[row]["MA_SO"].ToString().Trim() == "")
                {
                    //dgrNPC_BCKD00.Items.RemoveAt(dgrNPC_BCKD00.SelectedIndex);
                    dtbieu00.Rows.Remove(rowdel);
                }
                else
                {
                    kiemtraxoa(row);
                    if (dem1 > 1)
                    {
                        MessageBox.Show("Mã số " + dtbieu00.Rows[row]["MA_SO"].ToString().Trim() + " Đang tồn tại thư mục con không xóa được", "NPC-BCKD", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        if (MessageBox.Show("Anh(Chị) Có muốn xóa mã số " + dtbieu00.Rows[row]["MA_SO"].ToString().Trim() + " này ko?", "NPC-BCKD", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)
                        {
                            dtbieu00.Rows.Remove(rowdel);
                            //bus_TienichNPC.cls_TienichNPC_bus cls = new bus_TienichNPC.cls_TienichNPC_bus();
                            //cls.DELETE_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                            GlobalServices.Service_NPCUS.DELETE_DM(Convert.ToInt16(txtstt.Text), txtdvt.Text.ToString().Trim(), txtdanhmuc.Text.ToString().Trim(), txtmaso.Text.ToString().Trim(), strMa_dviqly);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                strTim = "";
                return;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Initgrid(strMa_dviqly);
            cmdsua.IsEnabled = true;
            cmdthemmoi.IsEnabled = true;
            cmdxoa.IsEnabled = true;
        }
        private void txtstt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtmaso.Focus();
            }
        }
        private void txtmaso_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtdvt.Focus();
            }
        }
        private void txtdvt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtdanhmuc.Focus();
            }
        }
        private void txtdanhmuc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmdghi.Focus();
            }
        }
        private void cmdthemmoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtstt.Focus();
            }
        }
        private void cmdsua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtdvt.Focus();
            }
        }


    }
}
